<?php
/**
 * GeoContexter
 * @link http://code.google.com/p/geocontexter/
 * @package GeoContexter
 */

/**
 * spatial search for record items
 *
 * @package GeoContexter
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author Armand Turpel <geocontexter@gmail.com>
 * @version $Rev: 153 $ / $LastChangedDate: 2010-02-18 17:21:41 +0100 (jeu., 18 févr. 2010) $ / $LastChangedBy: armand.turpel $
 */

namespace Gospecies\Controller;

use Zend\View\Model\ViewModel;
use Gospecies\Controller\AbstractIdentityJsonController;
use Zend\View\Model\JsonModel;

class AjaxGetUserProjectsJsonController extends AbstractIdentityJsonController
{

    /**
     */
    public function indexAction()
    {

        $user = $this->get_identity();

        $params = array('id_user' => $user->id_user);


        try {

          $RecordGet = $this->CoreModel('ProjectGetFromUser', 'Gospecies');


          $record  = $RecordGet->run( $params );

          if ($record instanceof \Core\Library\Exception) {
              return new JsonModel(array('error' => $record->getMessage()));

          }

          $ProjectGetAttributeGroups = $this->CoreModel('ProjectGetAttributeGroups');
          $ProjectGetLists           = $this->CoreModel('ProjectGetLists');

          foreach ($record as & $rec) {

            $params             = array('id_project' => $rec['id_project']);
            $rec['attr_groups'] = $ProjectGetAttributeGroups->run( $params );

            $params             = array('id_project' => $rec['id_project']);
            $rec['lists']       = $ProjectGetLists->run( $params );

          }

          return new JsonModel($record);

          } catch (\Exception $e) {

            $this->default_error = "Error\n File: " . __file__ . "\nLine: " . __line__ . "\nError: " . $e->getMessage();

            return new JsonModel(array('error' => $this->default_error));

          }

    }
}

